package cn.tedu.javagame.FirePlane;

import javax.swing.*;
import java.awt.*;
import java.util.List;
import java.util.ArrayList;

public class Enemy {
    int x, y;
    int speed;
    int fire, life;
    static Image enemyImg;
    static Image[] enemyImgs = new Image[6];
    int shootCount = 0; // 计数器，用于控制发射子弹的频率
    public static final int SHOOT_INTERVAL = 25; // 每0.5秒发射一次（500ms / 20ms = 25）

    //静态代码块
    static {
        enemyImg = new ImageIcon("plane/flys.png").getImage();
        for (int i = 0; i < enemyImgs.length; i++) {
            enemyImgs[i] = new ImageIcon("plane/flys" + i + ".png").getImage();
        }
    }

    public Enemy() {
        this.x = (int) (Math.random() * (ShootFrame.WIDTH - enemyImg.getWidth(null)));
        this.y = -enemyImg.getHeight(null);
        // 随机初始化速度，范围调整为1到3，使敌机变慢
        this.speed = (int) (Math.random() * 3 + 1);
        this.fire = 1;
        this.life = 3; // 设置敌机初始生命值为3
    }

    // 方法
    public void move() {
        this.y += speed;
    }

    int index = 0;

    public void step() {
        if (index == Integer.MAX_VALUE) {
            index = 0;
        }
        int i = index++ / 15 % enemyImgs.length;
        enemyImg = enemyImgs[i];
    }

    public List<enemyBullet> enemyshoot() {
        shootCount++;
        List<enemyBullet> enemyBullets = new ArrayList<>();
        if (shootCount % SHOOT_INTERVAL == 0) {
            int x = this.x + this.enemyImg.getWidth(null) / 2;
            int y = this.y + this.enemyImg.getHeight(null);
            enemyBullet bullet = new enemyBullet(x, y, this); // 创建 enemyBullet 对象，并传入当前敌机
            enemyBullets.add(bullet);
        }
        return enemyBullets;
    }
}